package com.hup.vd.view;

import com.hup.utils.commons.hardware.MouseUtil;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.ComponentUtil;
import com.hup.utils.swing.customView.HListView;
import com.hup.utils.swing.customView.ImageButton;
import com.hup.vd.view.customView.CreateTaskPanel;
import com.hup.vd.view.customView.RateLimitDialog;
import com.hup.vd.view.customView.RateLimitDialog.RateChangedListener;
import com.hup.vd.view.customView.TaskInfoPanel;
import com.hup.vd.view.customView.TaskItemView;
import org.apache.commons.io.FileUtils;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author hugan
 * @date 2022/1/11
 */
public class MainForm {
    public JPanel root;
    //
    public JLabel txtRateLimit;
    public JLabel txtCurrentRate;
    private RateLimitDialog rateLimitDialog;
    public JButton btnTray;
    public JCheckBox btnOnTop;
    //
    public JLabel labDownloading;
    public HListView<TaskItemView> lvDownloading;
    public JLabel labFinished;
    public HListView<TaskItemView> lvFinished;
    //
    public JTabbedPane tabbedPane;
    public TaskInfoPanel pTaskInfo;
    public CreateTaskPanel pCreateTask;
    public ImageButton btnRefresh;

    public MainForm() {
        $$$setupUI$$$();
        lvFinished.setVisible(false);
        txtCurrentRate.setText("");
        Container parent = lvFinished.getParent();
        Dimension dimension = new Dimension((int) (380 * LookThemeUtil.getScreenScale()), -1);
        parent.setPreferredSize(dimension);
        parent.setMinimumSize(dimension);
        parent.setMaximumSize(dimension);

        MouseAdapter maLabel = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (!MouseUtil.isLeftButton(e)) return;
                boolean visible = lvDownloading.isVisible();
                lvDownloading.setVisible(!visible);
                lvFinished.setVisible(visible);
                ComponentUtil.afterChangedChild(root);
            }
        };
        labDownloading.getParent().addMouseListener(maLabel);
        labFinished.getParent().addMouseListener(maLabel);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root = new JPanel();
        root.setLayout(new GridBagLayout());
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        panel1.setPreferredSize(new Dimension(150, 80));
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(panel1, gbc);
        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        lvDownloading = new HListView();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(lvDownloading, gbc);
        lvFinished = new HListView();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(lvFinished, gbc);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(panel2, gbc);
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        labDownloading = new JLabel();
        labDownloading.setHorizontalAlignment(2);
        labDownloading.setText("下载中");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 10;
        gbc.insets = new Insets(0, 5, 0, 0);
        panel2.add(labDownloading, gbc);
        txtCurrentRate = new JLabel();
        txtCurrentRate.setText("实时网速");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 0, 0, 8);
        panel2.add(txtCurrentRate, gbc);
        final JPanel spacer1 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel2.add(spacer1, gbc);
        btnRefresh = new ImageButton();
        btnRefresh.setShareIcon("/img/icon_refresh.png");
        btnRefresh.setToolTipText("刷新");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        panel2.add(btnRefresh, gbc);
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(panel3, gbc);
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        labFinished = new JLabel();
        labFinished.setHorizontalAlignment(2);
        labFinished.setText("已完成");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 10;
        gbc.insets = new Insets(0, 5, 0, 0);
        panel3.add(labFinished, gbc);
        tabbedPane = new JTabbedPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(tabbedPane, gbc);
        pTaskInfo = new TaskInfoPanel();
        tabbedPane.addTab("任务信息", pTaskInfo);
        pCreateTask = new CreateTaskPanel();
        tabbedPane.addTab("新建任务", pCreateTask);
        btnTray = new JButton();
        btnTray.setText("隐藏至托盘");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 8);
        root.add(btnTray, gbc);
        btnOnTop = new JCheckBox();
        btnOnTop.setFocusPainted(false);
        btnOnTop.setHorizontalTextPosition(2);
        btnOnTop.setText("置顶");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(0, 0, 0, 8);
        root.add(btnOnTop, gbc);
        txtRateLimit = new JLabel();
        txtRateLimit.setText(" 限速:无 ");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.ipadx = 10;
        gbc.insets = new Insets(5, 8, 0, 0);
        root.add(txtRateLimit, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root;
    }

    public void updateTaskCount(HListView<TaskItemView> listView) {
        if (listView == lvDownloading) {
            labDownloading.setText("下载中(" + listView.getChildCount() + ")");
        } else {
            labFinished.setText("已完成(" + listView.getChildCount() + ")");
        }
    }

    public void initRateLimit(Integer downloadRateLimit) {
        rateLimitDialog = new RateLimitDialog();
        if (downloadRateLimit == null) {
            txtRateLimit.setText(" 限速:无 ");
            rateLimitDialog.slRateLimit.setValue(0);
        } else {
            int index = rateLimitDialog.values.indexOf(downloadRateLimit);
            rateLimitDialog.slRateLimit.setValue(index);
            txtRateLimit.setText(" 限速:" + FileUtils.byteCountToDisplaySize(downloadRateLimit * 1024) + "/s");
        }
    }

    public void initRateLimitListener(RateChangedListener rateChangedListener) {
        rateLimitDialog.setRateChangedListener(rateChangedListener);
        txtRateLimit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        txtRateLimit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getClickCount() != 1 || !MouseUtil.isLeftButton(e)) return;
                rateLimitDialog.doShow(txtRateLimit);
            }
        });
    }

}
